#include "MainPage.h"

/**
 * @brief MainPage::MainPage
 * 主页面类实现
 */
MainPage::MainPage()
{
    qDebug() << "MainPage init";
    setupUI();  // 设置 UI
}

/**
 * @brief MainPage::setupUI
 * 初始化主页面ui
 */
void MainPage::setupUI()
{
    // 创建页面小部件
    this -> widget = new QWidget;
    QVBoxLayout * layout = new QVBoxLayout;

    SwipeStackWidget *stackedWidget = new SwipeStackWidget(this -> widget);

    // 创建按钮
    CardButton *music_button            = new CardButton(MAIN_PAGE_ICON_MUSIC, "音乐",          Route::Music);
    CardButton *navigation_button       = new CardButton(MAIN_PAGE_ICON_MAP, "导航",            Route::Navigation);
    CardButton *communication_button    = new CardButton(MAIN_PAGE_ICON_COMMUNICATION, "通信",  Route::Communication);
    CardButton *clock_button            = new CardButton(MAIN_PAGE_ICON_CLOCK, "时钟",          Route::Clock);
    CardButton *weather_button          = new CardButton(MAIN_PAGE_ICON_WEATHER, "天气",        Route::Weather);
    CardButton *video_button            = new CardButton(MAIN_PAGE_ICON_VIDEO, "视频",          Route::Video);
    CardButton *photo_button            = new CardButton(MAIN_PAGE_ICON_PHOTO,"图片",           Route::Photo);
    CardButton *browser_button          = new CardButton(MAIN_PAGE_ICON_BROWSER,"浏览器",       Route::Browser);

    // 第一页
    QWidget * page_1 = new QWidget;
    QHBoxLayout * hLayout_1 = new QHBoxLayout(page_1);
    hLayout_1 -> addWidget(music_button);
    hLayout_1 -> addWidget(navigation_button);
    hLayout_1 -> addWidget(communication_button);
    hLayout_1 -> addWidget(clock_button);

    page_1 -> setStyleSheet("background-color: #c0c0c0;border-radius: 15px;");
    stackedWidget -> addWidget(page_1);

    // 第二页
    QWidget * page_2 = new QWidget;
    QHBoxLayout * hLayout_2 = new QHBoxLayout(page_2);
    hLayout_2 -> addWidget(weather_button);
    hLayout_2 -> addWidget(video_button);
    hLayout_2 -> addWidget(photo_button);
    hLayout_2 -> addWidget(browser_button);

    page_2 -> setStyleSheet("background-color: #c0c0c0;border-radius: 15px;");
    stackedWidget -> addWidget(page_2);


    MyTimer *timerWidget = new MyTimer();

    // 添加页面指示器（小点）
    pageIndicatorLayout = new QHBoxLayout;
    pageIndicatorLayout->setAlignment(Qt::AlignCenter);
    for (int i = 0; i < 2; ++i) {
        QLabel *dotLabel = new QLabel(this -> widget);
        dotLabel->setText("•");
        dotLabel->setStyleSheet("font-size: 18px; color: darkgray;");
        pageIndicators.append(dotLabel);
        pageIndicatorLayout->addWidget(dotLabel);
    }
    updatePageIndicatorLayout(0);

    layout -> addWidget(timerWidget);
    layout -> addWidget(stackedWidget);
    layout -> addLayout(this -> pageIndicatorLayout);

    // 设置布局
    this -> widget->setLayout(layout);

    // 连接页面改变信号到槽函数
    QObject::connect(stackedWidget, &SwipeStackWidget::pageChanged, this, &MainPage::updatePageIndicatorLayout);
}

// 实现 MyPage 中的纯虚函数
QWidget* MainPage::getWidget()
{
    return this -> widget;
}


void MainPage::updatePageIndicatorLayout(int index)
{
    for (int i = 0; i < pageIndicators.size(); ++i) {
        if (i == index) {
            pageIndicators[i]->setStyleSheet("font-size: 18px; color: black;");
        } else {
            pageIndicators[i]->setStyleSheet("font-size: 18px; color: gray;");
        }
    }
}
